package com.example.demo.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.port.fee.dao.CustomerMapper;
import com.port.fee.entity.Customer;
import com.port.fee.service.CustomerService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 客户服务实现类
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Override
    public Customer getCustomerById(Long customerId) {
        return getById(customerId);
    }

    @Override
    public Long addDefaultCustomer() {
        Customer customer = new Customer();
        customer.setCustomerName("测试客户");
        customer.setCustomerCode("TEST001");
        customer.setContactPerson("测试联系人");
        customer.setContactPhone("13800138000");
        customer.setEmail("test@example.com");
        customer.setAddress("测试地址");
        customer.setCustomerType(1);
        customer.setCreateTime(new Date());
        customer.setUpdateTime(new Date());
        
        save(customer);
        return customer.getCustomerId();
    }
    
    @Override
    public Page<Customer> listCustomers(Customer customer, int pageNum, int pageSize) {
        // 构建查询条件
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        
        // 根据客户名称模糊查询
        if (StringUtils.isNotBlank(customer.getCustomerName())) {
            queryWrapper.like(Customer::getCustomerName, customer.getCustomerName());
        }
        
        // 根据客户编号查询
        if (StringUtils.isNotBlank(customer.getCustomerCode())) {
            queryWrapper.eq(Customer::getCustomerCode, customer.getCustomerCode());
        }
        
        // 根据联系人模糊查询
        if (StringUtils.isNotBlank(customer.getContactPerson())) {
            queryWrapper.like(Customer::getContactPerson, customer.getContactPerson());
        }
        
        // 根据联系电话查询
        if (StringUtils.isNotBlank(customer.getContactPhone())) {
            queryWrapper.like(Customer::getContactPhone, customer.getContactPhone());
        }
        
        // 根据客户类型查询
        if (customer.getCustomerType() != null) {
            queryWrapper.eq(Customer::getCustomerType, customer.getCustomerType());
        }
        
        // 按照创建时间降序排序
        queryWrapper.orderByDesc(Customer::getCreateTime);
        
        // 分页查询
        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }
    
    @Override
    public boolean addCustomer(Customer customer) {
        // 检查客户编号是否已存在
        if (StringUtils.isNotBlank(customer.getCustomerCode()) && 
            checkCustomerCodeExists(customer.getCustomerCode(), null)) {
            LOGGER.error("客户编号已存在: {}", customer.getCustomerCode());
            return false;
        }
        
        customer.setCreateTime(new Date());
        customer.setUpdateTime(new Date());
        
        return save(customer);
    }
    
    @Override
    public boolean updateCustomer(Customer customer) {
        // 检查客户编号是否已存在
        if (StringUtils.isNotBlank(customer.getCustomerCode()) && 
            checkCustomerCodeExists(customer.getCustomerCode(), customer.getCustomerId())) {
            LOGGER.error("客户编号已存在: {}", customer.getCustomerCode());
            return false;
        }
        
        customer.setUpdateTime(new Date());
        return updateById(customer);
    }
    
    @Override
    public boolean deleteCustomer(Long customerId) {
        // 先检查客户是否存在
        Customer customer = getById(customerId);
        if (customer == null) {
            return false;
        }
        
        // TODO: 检查客户是否被引用，如果被引用则不能删除
        
        return removeById(customerId);
    }
    
    @Override
    public boolean checkCustomerCodeExists(String customerCode, Long excludeId) {
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Customer::getCustomerCode, customerCode);
        
        // 如果是修改操作，排除当前ID
        if (excludeId != null) {
            queryWrapper.ne(Customer::getCustomerId, excludeId);
        }
        
        return count(queryWrapper) > 0;
    }
} 